home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / srtsltn / data1.cab / Target / Samples / ActiveX / SoSoX / sosoxDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-05  |  6.9 KB  |  253 lines

  1. // Copyright ⌐ 1997 Mario M. Westphal
  2. // All Rights reserved
  3. // This source code is only intended as a supplement to the
  4. // Sort Solution user documentation and related
  5. // electronic documentation provided with the library.
  6. // See these sources for detailed information regarding the
  7. // Sort Solution product.
  8. #include "stdafx.h"
  9. #include "sosox.h"
  10. #include "sosoxDlg.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18.  
  19. //###########################################################################
  20. // C S o s o x D l g 
  21. //###########################################################################
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // 
  25. CSosoxDlg::CSosoxDlg(CWnd* pParent /*=NULL*/)
  26.     : CDialog(CSosoxDlg::IDD, pParent)
  27. {
  28.     //{{AFX_DATA_INIT(CSosoxDlg)
  29.     m_Edit = _T("");
  30.     //}}AFX_DATA_INIT
  31.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  32.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  33.  
  34.     m_Running = false;
  35. }
  36.  
  37.  
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // 
  41. void CSosoxDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43.     CDialog::DoDataExchange(pDX);
  44.     //{{AFX_DATA_MAP(CSosoxDlg)
  45.     DDX_Control(pDX, IDC_PROGRESS_SORT, m_Progress_Sort);
  46.     DDX_Control(pDX, IDC_PROGRESS_MERGE, m_Progress_Merge);
  47.     DDX_Control(pDX, IDC_BTN_SORT, m_Btn_Sort);
  48.     DDX_Control(pDX, IDC_BTN_BROWSE, m_Btn_Browse);
  49.     DDX_Control(pDX, IDC_BTN_ABORT, m_Btn_Abort);
  50.     DDX_Text(pDX, IDC_EDIT, m_Edit);
  51.     DDX_Control(pDX, IDC_SORTSOL, m_SortSol);
  52.     //}}AFX_DATA_MAP
  53. }
  54.  
  55. BEGIN_MESSAGE_MAP(CSosoxDlg, CDialog)
  56.     //{{AFX_MSG_MAP(CSosoxDlg)
  57.     ON_WM_PAINT()
  58.     ON_WM_QUERYDRAGICON()
  59.     ON_BN_CLICKED(IDC_BTN_ABORT, OnBtnAbort)
  60.     ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
  61.     ON_BN_CLICKED(IDC_BTN_SORT, OnBtnSort)
  62.     //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64.  
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // 
  68. BOOL CSosoxDlg::OnInitDialog()
  69. {
  70.     CDialog::OnInitDialog();
  71.  
  72.     SetIcon(m_hIcon, TRUE);            // Set big icon
  73.     SetIcon(m_hIcon, FALSE);        // Set small icon
  74.     
  75.     m_Btn_Sort.EnableWindow(TRUE);
  76.     m_Btn_Abort.EnableWindow(FALSE);
  77.  
  78.     m_Progress_Sort.SetRange(0,100);
  79.     m_Progress_Merge.SetRange(0,100);
  80.  
  81.     return TRUE;  // return TRUE  unless you set the focus to a control
  82. }
  83.  
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // If you add a minimize button to your dialog, you will need the code below
  87. //  to draw the icon.  For MFC applications using the document/view model,
  88. //  this is automatically done for you by the framework.
  89. void CSosoxDlg::OnPaint() 
  90. {
  91.     if (IsIconic())
  92.     {
  93.         CPaintDC dc(this); // device context for painting
  94.  
  95.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  96.  
  97.         // Center icon in client rectangle
  98.         int cxIcon = GetSystemMetrics(SM_CXICON);
  99.         int cyIcon = GetSystemMetrics(SM_CYICON);
  100.         CRect rect;
  101.         GetClientRect(&rect);
  102.         int x = (rect.Width() - cxIcon + 1) / 2;
  103.         int y = (rect.Height() - cyIcon + 1) / 2;
  104.  
  105.         // Draw the icon
  106.         dc.DrawIcon(x, y, m_hIcon);
  107.     }
  108.     else
  109.     {
  110.         CDialog::OnPaint();
  111.     }
  112. }
  113.  
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // The system calls this to obtain the cursor to display while the user drags
  117. //  the minimized window.
  118. HCURSOR CSosoxDlg::OnQueryDragIcon()
  119. {
  120.     return (HCURSOR) m_hIcon;
  121. }
  122.  
  123. BEGIN_EVENTSINK_MAP(CSosoxDlg, CDialog)
  124.     //{{AFX_EVENTSINK_MAP(CSosoxDlg)
  125.     ON_EVENT(CSosoxDlg, IDC_SORTSOL, 1 /* SortPercentage */, OnSortPercentageSortsol, VTS_I2)
  126.     ON_EVENT(CSosoxDlg, IDC_SORTSOL, 2 /* MergePercentage */, OnMergePercentageSortsol, VTS_I2)
  127.     //}}AFX_EVENTSINK_MAP
  128. END_EVENTSINK_MAP()
  129.  
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // Avoid closing the dialog when a sort is running
  133. void CSosoxDlg::OnCancel() 
  134. {
  135.     if (!m_Running) CDialog::OnCancel();
  136. }
  137.  
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Avoid closing the dialog when a sort is running
  141. void CSosoxDlg::OnOK() 
  142. {
  143.     if (!m_Running) CDialog::OnOK();
  144. }
  145.  
  146.  
  147. /////////////////////////////////////////////////////////////////////////////
  148. // Open a file dialog to choose a profile
  149. void CSosoxDlg::OnBtnBrowse() 
  150. {
  151.     CFileDialog dlg(TRUE, _T("*.ssp"), NULL, OFN_OVERWRITEPROMPT,_T("Sort Solution Profiles (*.ssp)|*.ssp||"));
  152.  
  153.     if (dlg.DoModal() == IDOK) {
  154.         m_Edit = dlg.GetPathName();
  155.         UpdateData(FALSE);
  156.     }
  157. }
  158.  
  159.  
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162. // This method loads the profile specified in the edit field and runs
  163. // the sort
  164. void CSosoxDlg::OnBtnSort() 
  165. {
  166.     m_Btn_Sort.EnableWindow(FALSE);
  167.     m_Btn_Abort.EnableWindow(TRUE);
  168.  
  169.     m_Progress_Sort.SetPos(0);
  170.     m_Progress_Merge.SetPos(0);
  171.     m_Running = true;
  172.  
  173.     try {
  174.         
  175.         m_SortSol.LoadProfile(m_Edit);
  176.         m_SortSol.Sort();
  177.  
  178.         // If we land here, no error occured and the sort
  179.         // completed successfully
  180.         CString msg;
  181.         msg.Format(_T("Sort completed in %d seconds."),
  182.                    m_SortSol.GetStatsSortTime() + m_SortSol.GetStatsMergeTime());
  183.  
  184.         AfxMessageBox(msg,MB_ICONEXCLAMATION | MB_OK);
  185.     }
  186.     catch (CException* e)
  187.     {
  188.         e->ReportError(MB_ICONSTOP | MB_OK);
  189.         e->Delete();
  190.         m_Progress_Sort.SetPos(0);
  191.         m_Progress_Merge.SetPos(0);
  192.     }
  193.  
  194.     // The sort has finished
  195.     m_Btn_Sort.EnableWindow(TRUE);
  196.     m_Btn_Abort.EnableWindow(FALSE);
  197.     m_Running = false;
  198. }
  199.  
  200.  
  201. /////////////////////////////////////////////////////////////////////////////
  202. // 
  203. void CSosoxDlg::OnBtnAbort() 
  204. {
  205.     m_Running = false;
  206. }
  207.  
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210. // Handle the SortPercentage event
  211. void CSosoxDlg::OnSortPercentageSortsol(short Percentage) 
  212. {
  213.     m_Progress_Sort.SetPos(Percentage);
  214.     
  215.     // The user has aborted the sort. Set the "Aborted" property
  216.     // of the Sort Solution control to TRUE
  217.     if (!m_Running) m_SortSol.SetAborted(TRUE);
  218.  
  219.     // A CHEAP (and dirty) way to keep the app alive (and enables
  220.     // us to press the abort button)
  221.     // You should better use a second thread which manages the
  222.     // sort process and keeps the main app alive...
  223.     MSG msg;
  224.     while (::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE)) { 
  225.         AfxGetApp()->PumpMessage();
  226.     } 
  227.  
  228. }
  229.  
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232. // Handle the MergePercentage event
  233. void CSosoxDlg::OnMergePercentageSortsol(short Percentage) 
  234. {
  235.     m_Progress_Merge.SetPos(Percentage);
  236.  
  237.     // The user has aborted the sort. Set the "Aborted" property
  238.     // of the Sort Solution control to TRUE
  239.     if (!m_Running) m_SortSol.SetAborted(TRUE);
  240.  
  241.     
  242.     // A CHEAP (and dirty) way to keep the app alive (and enables
  243.     // us to press the abort button)
  244.     // You should better use a second thread which manages the
  245.     // sort process and keeps the main app alive...
  246.     MSG msg;
  247.     while (::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE)) { 
  248.         AfxGetApp()->PumpMessage();
  249.     } 
  250. }
  251.  
  252.  
  253.